package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.lanlian.cayman.LLApplication;
import com.lanlian.cayman.R;
import com.lanlian.cayman.fragment.RegisterFourFragment;
import com.lanlian.cayman.fragment.RegisterOneFragment;
import com.lanlian.cayman.fragment.RegisterThreeFragment;
import com.lanlian.cayman.fragment.RegisterTwoFragment;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqGetPreRegister;
import com.lanlian.commonlib.bean.RespPreRegister;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.uikit.ViewPagerSlide;

import java.util.ArrayList;


public class OpenAccentActivity extends BaseActivity implements OnClickListener {

    private RadioGroup mRgOpenAccount;
    private ViewPagerSlide mViewPager;
    private ImageView mImgBack;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    public static RespPreRegister respPreRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_accent);
        initView();
        setListeners();
        initFragments();
        getPreRegisterInfo();
    }

    @Override
    protected void initView() {
        super.initView();
        mRgOpenAccount = findViewById(R.id.rg_open_account);
        mViewPager = findViewById(R.id.view_pager);
        mImgBack = findViewById(R.id.img_back);
        mViewPager.setSlide(false);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mImgBack.setOnClickListener(this);
    }

    private void initFragments() {
        mFragments.add(new RegisterOneFragment(new RegisterOneFragment.OnNextClickListener() {
            @Override
            public void onNextClick() {
                RegisterTwoFragment fragment = (RegisterTwoFragment) mFragments.get(1);
                fragment.setPreRegisterInfo();
                mViewPager.setCurrentItem(1);
                mRgOpenAccount.check(R.id.rb_bank);
            }
        }));
        mFragments.add(new RegisterTwoFragment(new RegisterTwoFragment.OnPreviousClickListener() {
            @Override
            public void onNextClick() {
                RegisterThreeFragment fragment = (RegisterThreeFragment) mFragments.get(2);
                fragment.setPreRegisterInfo();
                mViewPager.setCurrentItem(2);
                mRgOpenAccount.check(R.id.rb_assets);
            }

            @Override
            public void onPreviousClick() {
                mViewPager.setCurrentItem(0);
                mRgOpenAccount.check(R.id.rb_id);
            }
        }));
        mFragments.add(new RegisterThreeFragment(new RegisterThreeFragment.OnPreviousClickListener() {
            @Override
            public void onNextClick() {
                RegisterFourFragment fragment = (RegisterFourFragment) mFragments.get(3);
                fragment.setPreRegisterInfo();
                mViewPager.setCurrentItem(3);
                mRgOpenAccount.check(R.id.rb_profession);
            }

            @Override
            public void onPreviousClick() {
                mViewPager.setCurrentItem(1);
                mRgOpenAccount.check(R.id.rb_bank);
            }
        }));
        mFragments.add(new RegisterFourFragment(new RegisterFourFragment.OnPreviousClickListener() {
            @Override
            public void onPreviousClick() {
                mViewPager.setCurrentItem(2);
                mRgOpenAccount.check(R.id.rb_assets);
            }
        }));
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return mFragments.get(i);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
        mRgOpenAccount.check(R.id.rb_id);
        mViewPager.setOffscreenPageLimit(mFragments.size());
    }

    /**
     * 获取预开户信息
     */
    public void getPreRegisterInfo() {
        ReqGetPreRegister reqGetPreRegister = new ReqGetPreRegister();
        reqGetPreRegister.setMobile(LLApplication.reqUserRegister.getMobile());
        VolleyRequestUtils.getInstance().getPreRegister(this.toString(), reqGetPreRegister, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                respPreRegister = (RespPreRegister) response;
                RegisterOneFragment fragment = (RegisterOneFragment) mFragments.get(0);
                fragment.setPreRegisterInfo();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.img_back:
                EventBusUtils.post(new EventMessage(EventCode.EVENT_PRE_REGISTER));
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        EventBusUtils.post(new EventMessage(EventCode.EVENT_PRE_REGISTER));
        super.onBackPressed();
    }
}
